package us.feng.tinyspring.beans.factory;

import us.feng.tinyspring.beans.factory.support.BeanDefinitionRegistry;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


import us.feng.tinyspring.beans.BeanDefinition;
import us.feng.tinyspring.beans.BeanException;
import us.feng.tinyspring.beans.BeanPostProcessor;
import us.feng.tinyspring.beans.PropertyValues;

public abstract class AbstractBeanFactory implements BeanFactory, BeanDefinitionRegistry {

	private Map<String,BeanDefinition> beanDefinitionMap=new ConcurrentHashMap<String,BeanDefinition>();
	
	/** BeanPostProcessors to apply in createBean */
	private final List<BeanPostProcessor> beanPostProcessors = new ArrayList<BeanPostProcessor>();

	public <T> Map<String,T> getBeansOfType(Class<T> type) {
		return null;
	}

	protected abstract void applyPropertyValues(String beanName, BeanDefinition mbd, Object bean, PropertyValues pvs);
	
	public abstract Object createBean(BeanDefinition bd,String beanName);
	
	/**
	 * 初始化所有单例Bean
	 */
	public abstract void preInstantiateSingletons();

	/**
	 * @see us.feng.tinyspring.beans.factory.support.BeanDefinitionRegistry#registerBeanDefinition(java.lang.String, us.feng.tinyspring.beans.BeanDefinition)
	 */
	public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
		this.beanDefinitionMap.put(beanName, beanDefinition);
	}


	/**
	 * @see us.feng.tinyspring.beans.factory.support.BeanDefinitionRegistry#removeBeanDefinition(java.lang.String)
	 */
	public void removeBeanDefinition(String beanName) {
		this.beanDefinitionMap.remove(beanName);
	}


	/**
	 * @see us.feng.tinyspring.beans.factory.support.BeanDefinitionRegistry#getBeanDefinition(java.lang.String)
	 */
	public BeanDefinition getBeanDefinition(String beanName) {
		return this.beanDefinitionMap.get(beanName);
	}


	@Override
	public int getBeanDefinitionCount() {
		return this.beanDefinitionMap.size();
	}
}
